/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;

/**
 *
 * @author Dry Bones
 */
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LeerEscribir {
    
    private void CargarobjCliente(Cliente obj,String linea){
        String[] parts = linea.split("\\|");
        obj.setId(Integer.parseInt(parts[0]));
        obj.setNombre(parts[1]);
        obj.setApellidoPaterno(parts[2]);
        obj.setApellidoMaterno(parts[3]);
        SimpleDateFormat formatter=new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
        String dateInString =parts[4];
        try{
            Date date = formatter.parse(dateInString);
            obj.setFechaNacimiento(date);
        }catch(Exception e){}
        obj.setSexo(parts[5]);
        obj.setCarnet(Integer.parseInt(parts[6]));
    }
    
    public ListaCliente Leer() {
        ListaCliente l=new ListaCliente();
        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        try {
            archivo = new File ("C:\\Users\\Dry Bones\\Desktop\\lista.txt");
            fr = new FileReader (archivo);
            br = new BufferedReader(fr);
            String linea;
            while((linea=br.readLine())!=null){
                System.out.println(linea);
                Cliente objCliente=new Cliente();
                CargarobjCliente(objCliente,linea);
                l.insertar(objCliente);
                linea = br.readLine();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{                    
                if( null != fr ){   
                    fr.close();     
                }                  
            }
            catch (Exception e2){ 
                e2.printStackTrace();
            }
        }
        return l;
    }

    public void Escribir(String objCliente){
        FileWriter fichero = null;
        PrintWriter pw = null;
        try{
            fichero = new FileWriter("C:\\Users\\Dry Bones\\Desktop/lista.txt",true);
            pw = new PrintWriter(fichero);
            pw.println(objCliente + "\n");
        }
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            try {
                if (null != fichero)
                    fichero.close();
            } 
            catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    public void Eliminar(int i){
        File fichero = null;
        FileReader fr = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        try{
            fichero = new File("C:\\Users\\Dry Bones\\Desktop/lista.txt");
            fr = new FileReader (fichero);
            pw = new PrintWriter(fichero);
            br = new BufferedReader(fr);
            String linea;
            while((linea=br.readLine())!=null){
                if (!linea.trim().equals(i)) {
                    pw.println(linea);
                    pw.flush();
                }
            }
            pw.close();
            br.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{                    
                if( null != fr ){   
                    fr.close();     
                }                  
            }
            catch (Exception e2){ 
                e2.printStackTrace();
            }
        }
    }
}

